'use strict';
import axios from 'axios';

function axAjax (url, method, params) {
	return axios({
		method: method,
		url: url,
		params: params,
		baseURL: 'http://bakari.cn:99/api',
		timeout: 2500,
		headers: {
			'Accept': 'application/json',
			'Content-Type': 'application/x-www-form-urlencoded'
		}
	}).then(res => {
		if (res.status === 200) {
			return res.data;
		}
	});
}

function formatDate (date, fmt) {
	if (/(y+)/.test(fmt)) {
		fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
	}

	let o = {
		'M+': date.getMonth() + 1,
		'd+': date.getDate(),
		'h+': date.getHours(),
		'm+': date.getMinutes(),
		's+': date.getSeconds()
	};
	for (let k in o) {
		if (new RegExp(`(${k})`).test(fmt)) {
			let str = o[k] + '';
			fmt = fmt.replace(RegExp.$1, str.padStart(2, '0'));
		}
	}
	return fmt;
}

function getQueryString (name) {
	let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
	let r = window.location.hash.substr(window.location.hash.indexOf('?') + 1).match(reg);
	if (r != null) return (r[2]);
	return null;
}

export {axAjax, formatDate, getQueryString};
